package com.xmtlxx.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.oms.domain.RfidDetectorConfig;
import com.xmtlxx.oms.domain.bo.RfidDetectorConfigBo;
import com.xmtlxx.oms.domain.vo.RfidDetectorConfigVo;
import com.xmtlxx.oms.mapper.RfidDetectorConfigMapper;
import com.xmtlxx.oms.service.IRfidDetectorConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 监控设备配置Service业务层处理
 *
 * @author xmtlxx
 * @date 2023-03-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RfidDetectorConfigServiceImpl implements IRfidDetectorConfigService {

    private final RfidDetectorConfigMapper baseMapper;

    /**
     * 查询监控设备配置
     *
     * @param id 监控设备配置主键
     * @return 监控设备配置
     */
    @Override
    public RfidDetectorConfigVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectRfidDetectorConfigVoByID(id);
    }

    /**
     * 查询监控设备配置列表
     *
     * @param bo 监控设备配置
     * @return 监控设备配置
     */
    @Override
    public TableDataInfo<RfidDetectorConfigVo> queryPageList(RfidDetectorConfigBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<RfidDetectorConfig> lqw = buildQueryWrapper(bo);
       // Page<RfidDetectorConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<RfidDetectorConfigVo> result = baseMapper.selectPageRfidDetectorConfigList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询监控设备配置列表
     *
     * @param bo 监控设备配置
     * @return 监控设备配置
     */
    @Override
    public List<RfidDetectorConfigVo> queryList(RfidDetectorConfigBo bo) {
       // LambdaQueryWrapper<RfidDetectorConfig> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectRfidDetectorConfigList(bo);
    }

    private LambdaQueryWrapper<RfidDetectorConfig> buildQueryWrapper(RfidDetectorConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RfidDetectorConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSn()), RfidDetectorConfig::getSn, bo.getSn());
        lqw.eq(bo.getInGpiNo() != null, RfidDetectorConfig::getInGpiNo, bo.getInGpiNo());
        lqw.eq(bo.getOutGpiNo() != null, RfidDetectorConfig::getOutGpiNo, bo.getOutGpiNo());
        lqw.eq(StringUtils.isNotBlank(bo.getAlertGpoNos()), RfidDetectorConfig::getAlertGpoNos, bo.getAlertGpoNos());
        lqw.eq(StringUtils.isNotBlank(bo.getReadGpoNos()), RfidDetectorConfig::getReadGpoNos, bo.getReadGpoNos());
        lqw.eq(bo.getCreateTime() != null, RfidDetectorConfig::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAntNos()), RfidDetectorConfig::getAntNos, bo.getAntNos());
        return lqw;
    }

    /**
     * 新增监控设备配置
     *
     * @param bo 监控设备配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(RfidDetectorConfigBo bo) {
        RfidDetectorConfig add = BeanUtil.toBean(bo, RfidDetectorConfig.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改监控设备配置
     *
     * @param bo 监控设备配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(RfidDetectorConfigBo bo) {
        RfidDetectorConfig update = BeanUtil.toBean(bo, RfidDetectorConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(RfidDetectorConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除监控设备配置
     *
     * @param ids 需要删除的监控设备配置主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
